package com.sx.mvvmlibs.base

import android.view.View
import android.widget.LinearLayout
import androidx.annotation.LayoutRes
import androidx.databinding.ViewDataBinding
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.sx.mvvmlibs.R
import com.sx.mvvmlibs.base.impl.IAppBar
import com.sx.mvvmlibs.config.GlobalConfig

/**
 * @Author: YY
 * @Date: 2022/1/12
 * @Describe: 带标题栏的 Activity
 */
abstract class AppBarBaseActivity<
        V : ViewDataBinding,
        VM : BaseViewModel<out BaseModel>,
        AppBarV : ViewDataBinding> (
    @LayoutRes layoutId: Int,
    varViewModelId: Int? = null,
    @LayoutRes private val appBarLayoutId: Int? = GlobalConfig.AppTitleBar.gAppBarLayoutId // 可以全局设置，也可以单独设置
) : DataBindingBaseActivity<V, VM>(layoutId, varViewModelId), IAppBar, OnTitleBarListener {

    private lateinit var mAppBarBinding: AppBarV

    private lateinit var mAppBarView : TitleBar

    override fun initContentView(contentView: View) {
        if (appBarLayoutId == null) {
            throw RuntimeException("appBarLayoutId 不可以是 null")
        }
        val pair: Pair<AppBarV, LinearLayout> = IAppBar.inflateRootLayout(this, contentView, appBarLayoutId)
        mAppBarBinding = pair.first

        mAppBarView = mAppBarBinding.root.findViewById(R.id.title_bar)
        mAppBarView.setOnTitleBarListener(this)

        initAppBar()
        super.initContentView(pair.second)
    }

    override fun onDestroy() {
        super.onDestroy()
        mAppBarBinding.unbind()
    }

    override fun onLeftClick(titleBar: TitleBar?) {
        finish()
    }

    open fun initAppBar() {

    }

    open fun getTitleBar(): TitleBar {
        return mAppBarView
    }
}